/**
 * 
 */
package org.mspring.platform.web.stat.filter;

/**
 * 
 * @author Gao Youbo
 * @since 2013-6-22
 */
public class StatFilterChain {
    /** 过滤类链中客户增加的最后一个过滤类 */
    private StatFilter lastFilter = null;

    /** 过滤类链中第一个过滤类 */
    private StatFilter firstFilter = null;

    ServiceContext context = new ServiceContext();

    public ServiceContext getContext() {
        return context;
    }

    public void setContext(ServiceContext context) {
        this.context = context;
    }

    /**
     * 构造一个过滤类链
     */
    public StatFilterChain() {
        firstFilter = lastFilter;
    }

    /**
     * 启动过滤类链的过滤
     * 
     * @param request
     *            Servlet的request请求
     * @param response
     *            Servlet的response应答
     */
    public void doFilter() {
        // try {
        if (firstFilter != null) {
            firstFilter.doFilter(context);
        }
        // } catch (BusinessException e) {
        // // 业务异常不可能传到这里
        // }
    }

    /**
     * 往过滤类链中增加过滤类
     * 
     * @param filter
     *            过滤类
     */
    public void addFilter(StatFilter filter) {
        if (firstFilter == null)
            firstFilter = filter;
        else
            lastFilter.setNextFilter(filter);

        lastFilter = filter;
    }
}
